#ifndef __PTGUI_FONT_H__
#define __PTGUI_FONT_H__

#include <ptgui/ptgui.h>
#include <ptgui/list.h>

struct ptgui_font;
struct ptgui_dc;
struct ptgui_rect;

struct ptgui_font_engine
{
	/* font engine function */
	void (*font_init)(struct ptgui_font* font);
	void (*font_load)(struct ptgui_font* font);

	void (*font_draw_text)(struct ptgui_font* font, struct ptgui_dc* dc, const rt_int8_t* text,
		rt_ubase_t len, struct ptgui_rect* rect);
	void (*font_get_metrics)(struct ptgui_font* font, const rt_int8_t* text, struct ptgui_rect* rect);
};

/*
 * bitmap font engine
 */
/* bitmap font private data */
struct ptgui_font_bitmap
{
	/* bitmap data */
	const rt_uint8_t* bmp;

	rt_uint16_t width;
	rt_uint16_t height;

	rt_uint8_t first_char;
	rt_uint8_t last_char;
};
extern struct ptgui_font_engine bmp_font_engine;

#include <ptgui/tree.h>
SPLAY_HEAD(cache_tree, hz_cache);
struct hz_cache
{
    SPLAY_ENTRY(hz_cache) hz_node;

    rt_uint16_t hz_id;
};

struct ptgui_hz_file_font
{
    struct cache_tree cache_root;
    rt_uint16_t cache_size;

    /* font size */
    rt_uint16_t font_size;
	rt_uint16_t font_data_size;

    /* file descriptor */
    int fd;

    /* font file name */
    const rt_int8_t* font_fn;
};
extern struct ptgui_font_engine ptgui_hz_file_font_engine;

struct ptgui_font
{
	/* font name */
	rt_int8_t* family;

	/* font height */
	rt_uint16_t height;

	/* refer count */
	rt_uint32_t refer_count;

	/* font engine */
	struct ptgui_font_engine* engine;

	/* font private data */
	void* data;

	/* the font list */
	ptgui_list_t list;
};
typedef struct ptgui_font ptgui_font_t;

void ptgui_font_system_init(void);
void ptgui_font_system_add_font(struct ptgui_font* font);
void ptgui_font_system_remove_font(struct ptgui_font* font);
struct ptgui_font* ptgui_font_default(void);
void ptgui_font_set_defaut(struct ptgui_font* font);

struct ptgui_font* ptgui_font_refer(const rt_uint8_t* family, rt_uint16_t height);
void ptgui_font_derefer(struct ptgui_font* font);

/* draw a text */
void ptgui_font_draw(struct ptgui_font* font, struct ptgui_dc* dc, const rt_int8_t* text, rt_ubase_t len, struct ptgui_rect* rect);
int  ptgui_font_get_string_width(struct ptgui_font* font, const rt_int8_t* text);
void ptgui_font_get_metrics(struct ptgui_font* font, const rt_int8_t* text, struct ptgui_rect* rect);

#endif
